$(document).ready(function(){
	// call enquiry action
	$('#form_enquiry').submit(function( event ) {
		event.preventDefault(); 
		//show here the loading display
		if(checkNameField() && checkEmailField()
			&& checkNumberField()
			&& checkEnquiryField()){
			$('#loading-indicator').show(); 
			var form = $('#form_enquiry').serialize();
			setTimeout(function() {
				$.ajax({
		   			url: "${pageContext.request.contextPath}/sendenquiry",
		   			type:"GET",
		   			data: form,
		   			success: function (response) {
		   				$('#loading-indicator').hide();
 		   				clearEnquiry();
		   				removeHaseSuccessAllField();
 		   				$('.information_notify').show();
	   					setTimeout("document.getElementById('information_notify').style.display='none';",5000);
		    		},
		   			error: function(jqXHR, textStatus, errorThrown){
		   				alert(textStatus);
		   			},
		   			async:false
		   		});
			}, 0);
		}
	});
	
	$( "#name_enquiry" ).blur(function() {
		checkNameField();
	});
	
	$( "#email_enquiry" ).blur(function() {
		checkEmailField();
	});
	
	$( "#number_enquiry" ).blur(function() {
		checkNumberField();
	});
	
	$( "#en_enquiry" ).blur(function() {
		checkEnquiryField();
	});
});

/* onblur form enquiry */
/* check name field */
function checkNameField(){
	var name = $("#name_enquiry").val();
	if($.trim(name).length != 0){
		$( "#form_group_name" ).removeClass( "has-error" ).addClass("has-success");
		$( "#name_glyphicon_ok" ).show();
		$( "#name_glyphicon_remove" ).hide();
		return true;
	}else{
		$( "#form_group_name" ).removeClass( "has-success" ).addClass("has-error");
		$( "#name_glyphicon_remove" ).show();
		$( "#name_glyphicon_ok" ).hide();
		return false;
	}
}
function checkEmailField(){
	var name = $("#email_enquiry").val();
	if($.trim(name).length != 0 && validateForm()){
		$( "#form_group_email" ).removeClass( "has-error" ).addClass("has-success");
		$( "#email_glyphicon_ok" ).show();
		$( "#email_glyphicon_remove" ).hide();
		$( "#validate_email_error" ).hide();
		return true;
	}else{
		$( "#form_group_email" ).removeClass( "has-success" ).addClass("has-error");
		$( "#email_glyphicon_remove" ).show();
		$( "#email_glyphicon_ok" ).hide();
		$( "#validate_email_error" ).show();
		return false;
	}
}

function checkNumberField(){
	var name = $("#number_enquiry").val();
	if($.trim(name).length != 0){
		$( "#form_group_number" ).removeClass( "has-error" ).addClass("has-success");
		$( "#number_glyphicon_ok" ).show();
		$( "#number_glyphicon_remove" ).hide();
		return true;
	}else{
		$( "#form_group_number" ).removeClass( "has-success" ).addClass("has-error");
		$( "#number_glyphicon_remove" ).show();
		$( "#number_glyphicon_ok" ).hide();
		return false;
	}
}

function checkEnquiryField(){
	var name = $("#en_enquiry").val();
	if($.trim(name).length != 0){
		$( "#form_group_enquiry" ).removeClass( "has-error" ).addClass("has-success");
		$( "#enquiry_glyphicon_ok" ).show();
		$( "#enquiry_glyphicon_remove" ).hide();
		return true;
	}else{
		$( "#form_group_enquiry" ).removeClass( "has-success" ).addClass("has-error");
		$( "#enquiry_glyphicon_remove" ).show();
		$( "#enquiry_glyphicon_ok" ).hide();
		return false;
	}
}

function removeHaseSuccessAllField(){
	$( "#form_group_name" ).removeClass( "has-success" );
	$( "#name_glyphicon_ok" ).hide();
	$( "#form_group_email" ).removeClass( "has-success" );
	$( "#email_glyphicon_ok" ).hide();
	$( "#form_group_number" ).removeClass( "has-success" );
	$( "#number_glyphicon_ok" ).hide();
	$( "#form_group_enquiry" ).removeClass( "has-success" );
	$( "#enquiry_glyphicon_ok" ).hide();
}

//clear text value
function clearEnquiry(){
	$('#name_enquiry').val('');
	$('#email_enquiry').val('');
	$('#number_enquiry').val('');
	$('#en_enquiry').val('');
};

// validate email
function validateForm() {
	var email = $("#email_enquiry").val();
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	  if(email != "" && emailReg.test( email )) {
	    return true;
	  } else {
	    return false;
	  }
}